/**
 * 版权所有(C)，上海勾芒信息科技，2016，所有权利保留。
 * 
 * 项目名：	carwx
 * 文件名：	MenuTreeBuilder.java
 * 模块说明：	
 * 修改历史：
 * 2016年12月2日 - Debenson - 创建。
 */
package com.alex.wechat.mp.menu.service;

import java.util.Comparator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alex.wechat.mp.config.WxConsts;
import com.alex.wechat.mp.menu.entity.WxMenuEntity;
import com.alex.wechat.mp.util.TextTemplateTool;
import com.google.common.base.Objects;

import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;

/**
 * 微信菜单树构造器。
 * 
 * @author Debenson
 * @since 0.1
 */
@Component
public class MenuTreeBuilder {

  @Autowired
  private TextTemplateTool templateTool;

  /**
   * 构造列表菜单树
   * 
   * @param source
   * @return
   */
  public List<WxMenuEntity> buildTree(List<WxMenuEntity> source) {
    WxMenuEntity virtualRoot = new WxMenuEntity();
    buildChildren(source, virtualRoot);
    List<WxMenuEntity> menus = virtualRoot.getChildren();
    if (menus.isEmpty()) {
      // 没有root，则直接返回原列表。
      menus = source;
      sortMenus(menus);
    }
    return menus;
  }

  /**
   * 取得微信菜单对象。
   * 
   * @param source
   * @return
   */
  public WxMenu buildWxMenuTree(List<WxMenuEntity> source) {
    WxMenu wxMenu = new WxMenu();

    // 先将其转换为菜单树
    final List<WxMenuEntity> treeMenus = buildTree(source);

    for (WxMenuEntity entity : treeMenus) {
      // 一级菜单
      final WxMenuButton button = convertToWxMenuButton(entity);
      wxMenu.getButtons().add(button);

      // 构造下级菜单
      for (WxMenuEntity subEntity : entity.getChildren()) {
        WxMenuButton subButton = convertToWxMenuButton(subEntity);
        button.getSubButtons().add(subButton);
      }
    }
    return wxMenu;
  }

  private WxMenuButton convertToWxMenuButton(WxMenuEntity sourceMenu) {
    WxMenuButton button = new WxMenuButton();
    button.setType(sourceMenu.getType());
    button.setName(sourceMenu.getName());
    button.setKey(sourceMenu.getMenuKey());
    if (WxConsts.BUTTON_VIEW.equals(sourceMenu.getType())) {
      button.setUrl(templateTool.resolve(sourceMenu.getUrl()));
    }
    return button;
  }

  private void buildChildren(List<WxMenuEntity> source, WxMenuEntity parent) {
    for (WxMenuEntity entity : source) {
      if (Objects.equal(parent.getId(), entity.getParentId())) {
        if (StringUtils.isNotBlank(parent.getId())) {
          entity.setParent(parent);
        }
        parent.getChildren().add(entity);
      }
    }
    sortMenus(parent.getChildren());
    for (WxMenuEntity child : parent.getChildren()) {
      buildChildren(source, child);
    }
  }

  private void sortMenus(List<WxMenuEntity> source) {
    java.util.Collections.sort(source, new Comparator<WxMenuEntity>() {

      @Override
      public int compare(WxMenuEntity o1, WxMenuEntity o2) {
        int order1 = Integer.valueOf(StringUtils.isBlank(o1.getOrders()) ? "0" : o1.getOrders());
        int order2 = Integer.valueOf(StringUtils.isBlank(o2.getOrders()) ? "0" : o2.getOrders());
        return order1 - order2;
      }

    });
  }
}
